<!-- 车型管理 -->
<template>
  <div class="dashboard-container car-models customer-list-box">
    <!-- 搜索 -->
    <div class="top-card">
      <el-card class="box-card">
        <el-form label-width="100px">
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item label="车型编号:">
                <el-input
                  placeholder="请输入车型编号"
                  size="medium"
                  v-model="carList.id"
                ></el-input> </el-form-item
            ></el-col>
            <el-col :span="8">
              <el-form-item label="应载重量：">
                <el-select
                  v-model="carList.allowableLoad"
                  placeholder="请选择应载重量"
                >
                  <el-option
                    v-for="item in weightList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                    size="medium"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="8">
              <el-form-item label="应载体积：">
                <el-select
                  v-model="carList.allowableVolume"
                  placeholder="请选择应载体积"
                >
                  <el-option
                    v-for="item in volumeList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                    size="medium"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="60">
            <el-col :span="8"
              ><el-form-item label="车辆类型：">
                <el-input
                  placeholder="请输入车辆类型"
                  size="medium"
                  v-model="carList.name"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button
                style="width:80px"
                @click="filterCarType"
                class="sousuo"
                >搜索</el-button
              >
              <el-button style="width:80px" @click="resetList" class="reset">
                重置</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
    <!-- 表格 -->
    <div class="bottom-card">
      <el-card class="box-card">
        <!-- 添加按钮 -->
        <el-button class="add-btn" @click="showDialog = true"
          >新增车型</el-button
        >
        <!-- 表格 -->
        <el-table
          :data="tableData"
          stripe
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column
            label="序号"
            type="index"
            :index="indexMethod"
            width="80px"
          ></el-table-column>
          <el-table-column
            label="车型编号"
            prop="id"
            min-width="180px"
          ></el-table-column>
          <el-table-column
            label="车辆类型"
            prop="name"
            min-width="130px"
          ></el-table-column>
          <el-table-column
            label="车型数量"
            prop="num"
            min-width="100px"
          ></el-table-column>
          <el-table-column
            label="应载重量（吨）"
            prop="allowableLoad"
            min-width="150px"
          ></el-table-column>
          <el-table-column
            label="应载体积（立方米）"
            prop="allowableVolume"
            min-width="150px"
          ></el-table-column>
          <el-table-column
            label="长（米）"
            prop="measureLong"
            min-width="100"
          ></el-table-column>
          <el-table-column
            label="宽（米）"
            prop="measureWidth"
            min-width="100px"
          ></el-table-column>
          <el-table-column
            label="高（米）"
            prop="measureHigh"
            min-width="100px"
          ></el-table-column>
          <el-table-column
            label="操作"
            fixed="right"
            width="110px"
            align="center"
          >
            <template v-slot="{ row }">
              <el-link type="primary" :underline="false" @click="edit(row.id)"
                >编辑</el-link
              >
              <span class="margin">|</span>
              <el-link type="danger" :underline="false" @click="del(row.id)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <div class="bottom-card">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="carList.page"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalNum"
          >
          </el-pagination>
        </div>
      </el-card>
    </div>
    <!-- 弹框组件 -->
    <CarModelsAdd
      ref="formRef"
      :showDialog="showDialog"
      @closeDialog="closeDialog"
      @update="list"
    ></CarModelsAdd>
    <!-- 删除询问弹窗 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>你确定要删除吗？</span>
      <span slot="footer">
        <el-button
          @click="dialogVisible = false"
          style="backgroundColor:#eee;color:black"
          >取 消</el-button
        >
        <el-button
          @click="delCarType"
          style="backgroundColor:#e15536;color:white"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getCarTypeList, list, del } from '@/api/transit'
import CarModelsAdd from './components/car-models-add.vue'
export default {
  components: {
    CarModelsAdd
  },
  data() {
    return {
      //应载重量
      weightList: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<1.8（吨）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '1.8-6（吨）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '6-14（吨）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '14-30（吨）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '30-50（吨）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '50-100（吨）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '100>（吨）'
        }
      ],
      //体积
      volumeList: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<3（m³）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '3-5（m³）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '5-10（m³）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '10-15（m³）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '15-30（m³）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '30-50（m³）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '50-80（m³）'
        },
        {
          value: 'RANGE_LEVEL_8',
          name: '80-150（m³）'
        },
        {
          value: 'RANGE_LEVEL_9',
          name: '150>（m³）'
        }
      ],
      carList: {
        // 准载重量
        allowableLoad: '',
        // 准载体积
        allowableVolume: '',
        //高
        measureHigh: '',
        // 长
        measureLong: '',
        //宽
        measureWidth: '',
        //车辆类型名称
        name: '',
        //页码
        page: 1,
        //页尺寸
        pageSize: 10,
        //车辆编码
        id: ''
      },
      //表格数据
      tableData: [],
      //总数
      totalNum: 0,
      //加载状态
      loading: false,
      //控制弹窗显示隐藏
      showDialog: false,
      //控制删除弹框的显示与隐藏
      dialogVisible: false,
      //
      currentId: 0
    }
  },
  //初始化
  created() {
    this.list()
    // this.getCarTypeList()
  },
  methods: {
    //渲染刷新表格数据
    async list() {
      // const res = await list({
      //   page: this.carList.page,
      //   pageSize: this.carList.pageSize
      // })
      const res = await list(this.carList)
      // console.log(res)
      this.tableData = res.data.items
      this.totalNum = +res.data.counts
    },
    //获取所有车辆类型数据
    async getCarTypeList() {
      const res = await getCarTypeList(this.carList)
      console.log(res)
      this.tableData = res.data
      this.totalNum = res.data.length
      console.log(this.totalNum)
    },
    //分页器每页都是数据函数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.carList.pageSize = +val
      this.list()
    },
    //分页器去往第几页函数
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.carList.page = +val
      this.list()
    },
    //序号函数
    indexMethod(index) {
      return index + 1 + (this.carList.page - 1) * 10
    },
    //搜索按钮
    filterCarType() {
      this.loading = true
      this.carList.page = 1
      this.list()
      console.log(this.carList)
    },
    //重置按钮
    resetList() {
      this.loading = true
      this.carList = {
        // 准载重量
        allowableLoad: '',
        // 准载体积
        allowableVolume: '',
        //高
        measureHigh: '',
        // 长
        measureLong: '',
        //宽
        measureWidth: '',
        //车辆类型名称
        name: '',
        //页码
        page: 1,
        //页尺寸
        pageSize: 10,
        //车辆编码
        id: ''
      }
      this.list()
    },
    //关闭弹窗
    closeDialog(value) {
      this.showDialog = value
    },
    //打开编辑弹窗
    edit(id) {
      this.showDialog = true
      this.$refs.formRef.detail(id)
    },
    //打开删除弹窗
    del(id) {
      this.currentId = id
      this.dialogVisible = true
    },
    //执行删除
    async delCarType() {
      await del(this.currentId)
      this.$message.success('删除成功')
      this.dialogVisible = false
      this.list()
    }
  },
  watch: {
    tableData() {
      this.loading = false
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.top-card {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  .box-card {
    width: 96%;
    border-radius: 10px;
    .sousuo {
      background-color: #e15536;
      color: #fff;
    }
    .sousuo:hover {
      background-color: #ffab98;
    }
    .reset {
      background-color: #fff;
      color: black;
      border: 1px solid #ccc;
    }
    .reset:hover {
      background-color: #ffeeeb;
      color: #e15536;
      border: 1px solid #e15536;
    }
  }
}
.bottom-card {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  padding-bottom: 20px;

  .box-card {
    width: 96%;
    border-radius: 10px;
    .add-btn {
      margin-bottom: 20px;
      background-color: #e15536;
      color: #fff;
    }
    .add-btn:hover {
      background-color: #ffab98;
    }
  }
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
  padding-bottom: 0px;
}

.el-select {
  width: 100%;
}

.car-models ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
.car-models {
  ::v-deep .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  ::v-deep .el-dialog__body {
    text-align: center;
    padding: 40px 60px 0 30px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}
</style>
